package xh.lumt.server;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by HanXiong on 2016/9/22.
 */
public class PlayStatusCenter {

    private PlayStatusInfo playStatusInfo;

    private StatusChangeListener[] listeners = new StatusChangeListener[0];

    private ScheduledExecutorService ses = new ScheduledThreadPoolExecutor(1);

    public PlayStatusCenter() {
        playStatusInfo = new PlayStatusInfo();
//        ses.scheduleAtFixedRate(new Runnable() {
//            @Override
//            public void run() {
//                synchronized (PlayStatusCenter.this) {
//                    if (playStatusInfo.getStartPlayTime() > 0) {
//
//                        if (System.currentTimeMillis() - playStatusInfo.getStartPlayTime() > ) {
//
//                        }
//                    }
//
//
//                }
//            }
//        }, 1000, 500, TimeUnit.MILLISECONDS);
    }

    public void listenStatusChange(StatusChangeListener l) {
        StatusChangeListener[] newL = new StatusChangeListener[listeners.length + 1];
        System.arraycopy(listeners, 0, newL, 0, listeners.length);
        newL[listeners.length] = l;
        listeners = newL;
    }



    // 播放模式

    public void update(PlayingUpdateInfo ui) {
        boolean hasUpdate = false;
        PlayStatusInfo pi = null;

        synchronized (this) {
            if (ui.getMusicUrl() != null && !ui.getMusicUrl().equals(playStatusInfo.getMusicUrl())) {
                playStatusInfo.setMusicUrl(ui.getMusicUrl());
                hasUpdate = true;
            }

            if (ui.getVolume() != null && !ui.getVolume().equals(playStatusInfo.getVolume())) {
                playStatusInfo.setVolume(ui.getVolume());
                hasUpdate = true;
            }

            // TODO

            playStatusInfo.setUpdateTime(System.currentTimeMillis());
            if (hasUpdate) {
                pi = playStatusInfo.copy();
            }
        }
        if (hasUpdate) {
            fireStatusChange(pi);
        }
    }

    public void start(String url, double fromSecond) {
        PlayStatusInfo pi;
        synchronized (this) {
            playStatusInfo.setMusicUrl(url);
            playStatusInfo.setStartPlayFromSecond(fromSecond);
            playStatusInfo.setStartPlayTime(System.currentTimeMillis());
            playStatusInfo.setPlaying(true);
            playStatusInfo.setUpdateTime(System.currentTimeMillis());
            pi = playStatusInfo.copy();
        }
        fireStatusChange(pi);
    }

    public void pause() {
        PlayStatusInfo pi;
        synchronized (this) {
            playStatusInfo.setPlaying(false);
            playStatusInfo.setUpdateTime(System.currentTimeMillis());
            pi = playStatusInfo.copy();

        }
        fireStatusChange(pi);
    }

    public void updatePlayPosition(int second) {

    }

    public synchronized PlayStatusInfo checkNewestStatus(long t) {
        if (t < playStatusInfo.getUpdateTime()) {
            return playStatusInfo.copy();
        } else {
            return null;
        }
    }

    private void fireStatusChange(PlayStatusInfo pi) {
        for (StatusChangeListener l : listeners) {
            l.onChange(pi);
        }
    }

    interface StatusChangeListener {
        void onChange(PlayStatusInfo p);
    }
}
